package com.md.notes.model.entity;

public class Note {
    private String title;
    private String createDate;
    private String specNo;
    private String content;
    private String appNo;

    // 构造函数
    public Note() {
    }

    // Getters and Setters
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public String getCreateDate() { return createDate; }
    public void setCreateDate(String createDate) { this.createDate = createDate; }
    public String getSpecNo() { return specNo; }
    public void setSpecNo(String specNo) { this.specNo = specNo; }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    public String getAppNo() {
        return appNo;
    }

    public void setAppNo(String appNo) {
        this.appNo = appNo;
    }

    // 解析方法
    public static Note parseFromString(String content) {
        String title = null;
        String createDate = null;
        String specNo = null;
        String appNo = null;

        // 分割字符串获取 front matter 部分
        String[] lines = content.split("\n");
        boolean isFrontMatter = false;

        for (int i=0;i<lines.length;i++) {
            String line=lines[i].trim();
            if (line.equals("---")) {
                isFrontMatter = !isFrontMatter;
                continue;
            }

            if (isFrontMatter) {
                String[] parts = line.split(":", 2);
                if (parts.length == 2) {
                    String key = parts[0].trim();
                    String value = parts[1].trim();

                    switch (key) {
                        case "title":
                            title = value;
                            break;
                        case "createDate":
                            createDate = value;
                            break;
                        case "specNo":
                            specNo = value;
                            break;
                        case "appNo":
                            appNo = value;
                            break;
                    }
                }
            }
        }
        Note note = new Note();
        note.setTitle(title);
        note.setCreateDate(createDate);
        note.setSpecNo(specNo);
        note.setAppNo(appNo);
        return note;
    }
}
